﻿using System;
using System.Web;
using System.Collections;

namespace AspDIY.Net.Common
{
    /// <summary>
    /// Cache操作类
    /// </summary>
    public class ADCache
    {

        #region 缓存相关类
        /// <summary>
        /// 获取指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey">Cache名</param>
        /// <returns>Cache值</returns>
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }
        /// <summary>
        /// 获取所有CacheKey
        /// </summary>
        public static ArrayList GetAllCache()
        {
            ArrayList ArrCacheKey = new ArrayList();
            System.Collections.IDictionaryEnumerator coll = HttpRuntime.Cache.GetEnumerator();
            while (coll.MoveNext() && coll.Current != null)
                ArrCacheKey.Add(coll.Key);
            return ArrCacheKey;
        }
        /// <summary>
        /// 删除指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey">Cache键</param>
        /// <returns>true或false</returns>
        public static bool RemoveCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Remove(CacheKey);
            if (ADCache.GetCache(CacheKey) != null) return false;
            else return true;
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey">Cache名</param>
        /// <param name="objObject">Cache值</param>
        /// <returns>无</returns>
        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject);
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值(绝对过期时间 or 相对过期时间)
        /// </summary>
        /// <param name="CacheKey">Cache名</param>
        /// <param name="objObject">Cache值</param>
        /// <param name="absoluteExpiration">绝对过期时间</param>
        /// <param name="slidingExpiration">相对过期时间</param>
        /// <returns>无</returns>
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }
        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值(依赖文件相对路径)
        /// </summary>
        /// <param name="CacheKey">键</param>
        /// <param name="objObject">值</param>
        /// <param name="filepath">依赖文件相对路径</param>
        /// <returns>无</returns>
        public static void SetCache(string CacheKey, object objObject, string filepath)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, new System.Web.Caching.CacheDependency(HttpContext.Current.Server.MapPath(filepath)));
        }
        #endregion

    }
}
